# 安装模块
# pip install qcloudsms_py



from qcloudsms_py import SmsSingleSender
from qcloudsms_py.httpclient import HTTPError
from . import settings
import random
from luff.uitls.logg import logger
# 需要发送短信的手机号码
phone_numbers = ["15203664920",]

def get_code():
    code=''
    for i in range(6):
        code += str(random.randint(0, 9))
    return code

def send(phone,code):
    ssender = SmsSingleSender(settings.APPID, settings.APPKEY)
    # params = ["5678"]  # 当模板没有参数时，`params = []`
    try:
      result = ssender.send_with_param(86, phone,
          settings.TEMPLATE_ID, [code,], sign=settings.SMS_SIGN, extend="", ext="")
      if result.get('result') == 0:
          return True
      else:
        logger.error('发送失败')
        return False

    except Exception as e:
      logger.error('发送异常')
      return False
